package com.net.tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

public class DownloadToole {
	HttpURLConnection _httpConnection;

	public static final String path = Environment.getExternalStorageDirectory()
			.getPath() + "/.dailyyoga/usericon/";

	public static String getURLName(String URl) {

		String name = "";

		int subIndex = URl.lastIndexOf("/") + 1;

		name = URl.substring(subIndex);
		URl = URl.substring(0, subIndex - 1);

		subIndex = URl.lastIndexOf("/") + 1;

		name = URl.substring(subIndex) + name;

		Log.d("getURLName", name);

		return name;

	}

	public void download(String URl, String fileName) throws IOException {

		synchronized (DownloadToole.class) {

			try {
				downloadBitMap(URl + "_s", fileName + "_s");
			} catch (Exception e) {
				downloadBitMap(URl, fileName);
				saveSmallBitmap(fileName);
			}

		}
	}

	public void downloadBigBitMap(String URl, String fileName)
			throws IOException {
		synchronized (DownloadToole.class) {
			downloadBitMap(URl, fileName);
		}
	}

	public File getRootPathFile() {
		File file = new File(path);

		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}

	private void saveSmallBitmap(String fileName) throws IOException {
		String small = fileName + "_s";
		File smallFile = new File(path + small);
		smallFile.createNewFile();

		Bitmap src = BitmapFactory.decodeFile(path + fileName);
		int w = src.getWidth();
		int h = src.getHeight();
		float k = w > h ? (90.0f / w) : (90.0f / h);
		Bitmap smallBitmap = Bitmap.createScaledBitmap(src, (int) (w * k),
				(int) (h * k), true);

		OutputStream outputStream = new FileOutputStream(smallFile);

		smallBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
		outputStream.flush();
		outputStream.close();

	}

	public void replaceUserIcon(InputStream inputStream, String name)
			throws IOException {

		synchronized (DownloadToole.class) {
			Bitmap src = BitmapFactory.decodeStream(inputStream);
			int w = src.getWidth();
			int h = src.getHeight();
			Log.d("replaceUserIcon", "sh=" + h);
			Log.d("replaceUserIcon", "sw=" + w);
			// float k = w > h ? (90.0f / w) : (90.0f / h);
			Bitmap smallBitmap = Bitmap.createScaledBitmap(src, w, h, true);

			Log.d("replaceUserIcon", "h=" + smallBitmap.getHeight());
			Log.d("replaceUserIcon", "w=" + smallBitmap.getWidth());

			File file = new File(path);

			if (!file.exists()) {
				file.mkdirs();
			}
			File smallFile = new File(path + name);
			smallFile.createNewFile();
			OutputStream outputStream = new FileOutputStream(smallFile);

			smallBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
			outputStream.flush();
			outputStream.close();
		}

	}

	private void downloadBitMap(String URl, String fileName) throws IOException {

		byte[] _buffer = new byte[1024];
		File file = new File(path);

		if (!file.exists()) {
			file.mkdirs();
		}

		file = new File(file, "tmp");

		if (!file.exists()) {
			file.createNewFile();
		}

		file = new File(path + fileName);

		if (!file.exists()) {
			_httpConnection = (HttpURLConnection) new URL(URl).openConnection();
			InputStream inputStream = _httpConnection.getInputStream();

			Log.d("rename", "icon size=" + inputStream.available());
			OutputStream outputStream = new FileOutputStream(new File(path
					+ "tmp"));
			int offset = 0;

			while ((offset = inputStream.read(_buffer)) != -1) {
				outputStream.write(_buffer, 0, offset);
			}
			inputStream.close();
			outputStream.close();

			Log.d("rename", "rename>>>>>>>>>>>>" + file.getAbsolutePath()
					+ new File(path + "tmp").renameTo(file));
		}

	}

	public static Bitmap getBigBitmap(String fileName) {
		try {

			FileInputStream fileInputStream = new FileInputStream(path
					+ fileName);
			Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
			fileInputStream.close();

			return bitmap;
		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;

	}

	public Bitmap getBitmap(String fileName) throws IOException {

		FileInputStream fileInputStream = new FileInputStream(path + fileName
				+ "_s");
		Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
		fileInputStream.close();

		if (bitmap == null) {

			new File(path + fileName + "_s").delete();

			throw new IOException("bitmap == null");
		}

		return bitmap;

	}

	public void cancal() {

		if (_httpConnection != null) {
			_httpConnection.disconnect();
		}

	}

	public void clear(final HashMap<String, String> buffer) {

		new Thread() {

			public void run() {

				try {
					File file = new File(path);

					for (File element : file.listFiles()) {

						if (buffer.get(element.getName()) == null) {

							Log.d("delete",
									element.getName() + element.canWrite()
											+ element.delete());

						} else {
							Log.d("delete", element.getName() + "����");
						}

					}
				} catch (Exception e) {
					// TODO: handle exception
				}

			}

		}.start();

	}

}
